package impl

import (
	"adam2/internal/model"
)

type ConceptBoardIndexDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetConceptBoardIndexDaoImpl() *ConceptBoardIndexDaoImpl {
	return &ConceptBoardIndexDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) FindTableName() string {
	return "CONCEPT_BOARD_INDEX"
}

// 获取最近记录
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) FindLatestConceptBoardIndex() model.ConceptBoardIndex {
	var conceptBoardIndex model.ConceptBoardIndex
	var sql string = "select * from (" +
		"select * from concept_board_index t order by t.transaction_date desc) t1 " +
		"where rownum<=1"
	_conceptBoardIndexDaoImpl.db.Raw(sql).Scan(&conceptBoardIndex)
	return conceptBoardIndex
}

// 前一个交易日的日期
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) PreviousTransactionDate(transactionDate string) string {
	var previousDate string
	var sql string = "select * from (" +
		"select DISTINCT to_char(t.TRANSACTION_DATE, 'yyyy-mm-dd') " +
		"from concept_board_index t " +
		"where t.TRANSACTION_DATE<to_date(?,'yyyy-mm-dd') " +
		"order by to_char(t.TRANSACTION_DATE, 'yyyy-mm-dd') desc) t1 " +
		"where rownum<=1"
	_conceptBoardIndexDaoImpl.db.Raw(sql, transactionDate).Scan(&previousDate)
	return previousDate
}

// 后一个交易日的日期
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) NextTransactionDate(transactionDate string) string {
	var nextDate string
	var sql string = "select * from (" +
		"select DISTINCT to_char(t.TRANSACTION_DATE, 'yyyy-mm-dd') " +
		"from concept_board_index t " +
		"where t.TRANSACTION_DATE>to_date(?,'yyyy-mm-dd') " +
		"order by to_char(t.TRANSACTION_DATE, 'yyyy-mm-dd') asc) t1 " +
		"where rownum<=1"
	_conceptBoardIndexDaoImpl.db.Raw(sql, transactionDate).Scan(&nextDate)
	return nextDate
}

// 根据开始时间和结束时间，计算平均KD值
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) FindAverageKDByBeginDateAndEndDate(beginDate string, endDate string) model.ConceptBoardIndexArray {
	var conceptBoardIndexArray model.ConceptBoardIndexArray
	var sql string = "select t.transaction_date, avg(t.k) k, avg(t.d) d " +
		"from concept_board_index t " +
		"where t.TRANSACTION_DATE between to_date(?,'yyyy-mm-dd HH24:mi:ss') and to_date(?,'yyyy-mm-dd HH24:mi:ss') " +
		"group by t.transaction_date " +
		"order by t.transaction_date asc"
	_conceptBoardIndexDaoImpl.db.Raw(sql, beginDate, endDate).Scan(&conceptBoardIndexArray)
	return conceptBoardIndexArray
}

// 根据开始时间和结束时间，查询某个概念板块的KD值
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) FindKDByMyquantCodeAndBeginDateAndEndDate(myquantCode string, beginDate string, endDate string) model.ConceptBoardIndexArray {
	var conceptBoardIndexArray model.ConceptBoardIndexArray
	var sql string = "select * " +
		"from concept_board_index t " +
		"where t.TRANSACTION_DATE between to_date(?,'yyyy-mm-dd HH24:mi:ss') and to_date(?,'yyyy-mm-dd HH24:mi:ss') " +
		"and t.myquant_code=? " +
		"order by t.transaction_date asc"
	_conceptBoardIndexDaoImpl.db.Raw(sql, beginDate, endDate, myquantCode).Scan(&conceptBoardIndexArray)
	return conceptBoardIndexArray
}

// 根据myquant_code、begin_date和end_date，查询记录，并按照transaction_date列升序排列
func (_conceptBoardIndexDaoImpl *ConceptBoardIndexDaoImpl) FindByMyquantCodeAndBeginDateAndEndDateOrderByTransactionDateAsc(myquantCode string, beginDate string, endDate string) model.ConceptBoardIndexArray {
	var conceptBoardIndexArray model.ConceptBoardIndexArray
	var sql string = "select * from CONCEPT_BOARD_INDEX t where t.MYQUANT_CODE=? " +
		"and t.TRANSACTION_DATE BETWEEN to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd') " +
		"order by t.TRANSACTION_DATE asc"
	_conceptBoardIndexDaoImpl.db.Raw(sql, myquantCode, beginDate, endDate).Scan(&conceptBoardIndexArray)
	return conceptBoardIndexArray
}
